﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ProjectPheonix {
    class BasicEntity {
        public Model model { get; protected set; }
        protected Matrix world = Matrix.Identity;
        public Vector3 position;

        protected Matrix scaling = new Matrix();
        float scale = 1.0f;

        public BasicEntity(Model m, Vector3 pos, float s) {
            model = m;
            position = pos;
            scale = s;
        }

        public virtual void Update() {

        }

        public void Draw(Player player) {
            scaling = Matrix.CreateScale(scale);

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes) {
                foreach (BasicEffect bEffect in mesh.Effects) {
                    bEffect.EnableDefaultLighting();
                    bEffect.Projection = player.projection;
                    bEffect.View = player.view;
                    bEffect.World = (scaling * GetWorld() * mesh.ParentBone.Transform);
                    bEffect.World *= Matrix.CreateTranslation(position);
                }
                mesh.Draw();
            }
        }

        public bool CollidesWith(Model otherModel, Matrix otherWorld) {
            foreach (ModelMesh myModelMeshes in model.Meshes) {
                foreach (ModelMesh hisModelMeshes in otherModel.Meshes) {
                    if (myModelMeshes.BoundingSphere.Transform(
                        GetWorld()).Intersects(
                        hisModelMeshes.BoundingSphere.Transform(otherWorld)))
                        return true;
                }
            }
            return false;
        }

        public virtual Matrix GetWorld() {
            return world;
        }
    }
}
